using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom.Compiler;
using System.IO;
using System.Configuration;
using System.CodeDom;

namespace CommonGenerator
{
	public enum Language
	{
		CSharp,
		VB
	}
	public abstract class CommonGenerator
	{
		string _ConfigFile;
		public CommonGenerator()
		{
			NameSpace = "";
		}

		public String ConfigFile
		{
			get
			{
				return _ConfigFile;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");
				if(!File.Exists(value))
					throw new FileNotFoundException(value, value);
				_ConfigFile = value;
			}
		}
		public Configuration ConfigurationObject
		{
			get
			{
				return ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
				{
					ExeConfigFilename = ConfigFile
				}, ConfigurationUserLevel.None);
			}
		}

		public String NameSpace
		{
			get;
			set;
		}

		public String ClassName
		{
			get;
			set;
		}

		public Language Language
		{
			get;
			set;
		}

		protected CodeNamespace CreateCodeNamespace()
		{
			var ns = new CodeNamespace(NameSpace);
			CodeTypeDeclaration type = new CodeTypeDeclaration(ClassName);
			ns.Types.Add(type);
			return ns;
		}


		protected CodeDomProvider CreateProvider()
		{
			switch(Language)
			{
				case Language.CSharp:
					return new Microsoft.CSharp.CSharpCodeProvider();
				case Language.VB:
					return new Microsoft.VisualBasic.VBCodeProvider();
				default:
					throw new NotSupportedException();
			}
		}
		public abstract void WriteClass(TextWriter writer);
	}
}
